#ifndef FIR_FIRRENDERER_H_
#define FIR_FIRRENDERER_H_

#include <string>
#include <dwmapi.h>
#include <GdiPlus.h>
#include "Window.h"
#include "resource.h"
#include "firkey.h"
#include "firpos.h"
#include "firplayer.h"
#include "firmanager.h"

// lib
#pragma comment(lib, "gdiplus.lib")
#pragma comment(lib, "dwmapi.lib")
#pragma comment(lib, "Msimg32.lib")
#pragma comment(lib, "comctl32.lib")

// using microsoft windows common controls 6.0
#pragma comment(linker, "\"/manifestdependency:type='Win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='X86' publicKeyToken='6595b64144ccf1df' language='*'\"")

#define REFERENCE_PARAM(m) (m)

class FIRRenderer : public Window
{
    // some type definition
    typedef unsigned char	Byte;
    typedef COLORREF		Color;

private:
    static FIRRenderer	instance;

    // some const values of table and chess;
    static const long kTableBmpWidth;
    static const long kTableBmpHeight;
    static const long kChessWidth;
    static const long kChessHeight;
    static const long kTableLeft;
    static const long kTableTop;
    static const long kGridLength;
    static const long kGridCount;
    static const long kHalfChess;
    static const long kHalfGrid;

    // the current position of sight
    static long kCurrentSightPositionX;
    static long kCurrentSightPositionY;

    // last down chess position
    static long kLastDownChessPositionX;
    static long kLastDownChessPositionY;

    // some variables
    HBITMAP				table;
    HBITMAP				black;
    HBITMAP				white;
    HDC					mem;
    HBITMAP				bitmap;
    HDC					background;
    ULONG_PTR			gditoken;
    bool				user_control;


public:
    FIRRenderer()
    {
        Gdiplus::GdiplusStartupInput gsi;
        Gdiplus::GdiplusStartup(&gditoken, &gsi, 0);
    }
    ~FIRRenderer()
    {
        Gdiplus::GdiplusShutdown(gditoken);
        if (background)
        {
            DeleteDC(background);
        }
    }
    static FIRRenderer& Instance(void);

    static INT_PTR CALLBACK NewGameMessageHandler(HWND hDlg, 
        UINT message, WPARAM wParam, LPARAM lParam);

    bool Init(HINSTANCE hInst);
    void NewGame(void);
    void NewGameInit(void);
    void DrawTable(void);
    void DrawCopyright(void);
    void DrawCoordinate(void);
    void DrawChess(Byte matrix[15][15]);
    void DrawChessman  (int x, int y, int color);
    void DrawNewSight(void);
    void DrawLastSight(void);
    void EnableUserControl(void);
    void DisableUserControl(void);
    bool IsInUserControl(void);
    int MessageBox( LPCTSTR lpText, LPCTSTR lpCaption, UINT uType );

protected:
    void DrawBmp(RECT& rect, HBITMAP hbmp );

    void DrawSight(long x, long y, Color color);

    Gdiplus::Image* GetPngImagePtr( DWORD id );

    HBITMAP Create32BPPDIBSection(HDC hDC, int iWidth, int iHeight);

    HBITMAP FromImage(Gdiplus::Image* pImage);

    // Messages
    virtual void OnPaint();
    virtual void OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags);
    virtual void OnKeyUp(UINT nChar, UINT nRepCnt, UINT nFlags);
    virtual void OnLButtonDown(UINT nFlags, Point point );
    virtual void OnMouseMove(UINT nFlags, Point point);


    // Messages of dialog
    static INT_PTR OnInitDialog(HWND hDlg, WPARAM wParam, LPARAM lParam);
    static INT_PTR OnCommond(HWND hDlg, WPARAM wParam, LPARAM lParam);
    static INT_PTR OnOK(HWND hDlg, WPARAM wParam, LPARAM lParam);
    static INT_PTR OnCancel(HWND hDlg, WPARAM wParam, LPARAM lParam);

    static void ChessDown( int player , FIRMove move);

    void BeginDraw(void);
    void EndDraw(void);
    void Save(void);
    void Load(void);
    bool WindowPointToChessTablePos(Point point, Pos& pos);
};

#endif // FIR_FIRENDERER_H_